using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;

using DotNetNuke.Security;

namespace YourCompany.Modules.Cafeteria
{
	public partial class ViewCafeteria : PortalModuleBase, IActionable
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			try
			{
				Label6.Text = DotNetNuke.Entities.Users.UserController.GetCurrentUserInfo().DisplayName;
				if (!PortalSecurity.IsInRole("Registered Users"))
				{
					this.Announcements.Visible = false;
					this.OrderProducts.Visible = false;
					this.OrderHistory.Visible = false;
					this.GridView2.Visible = false;
					this.Label1.Visible = false;
					this.Label2.Visible = false;
					this.Label3.Visible = false;
					this.TextBox1.Visible = false;
					this.TextBox2.Visible = false;
					this.TextBox3.Visible = false;
					this.Button1.Visible = false;
					this.Button2.Visible = false;
				}

			}
			catch (Exception ex)
			{
				Exceptions.ProcessModuleLoadException(this, ex);
			}
		}

		#region IActionable Members

		public DotNetNuke.Entities.Modules.Actions.ModuleActionCollection ModuleActions
		{
			get
			{
				//create a new action to add an item, this will be added to the controls
				//dropdown menu
				ModuleActionCollection actions = new ModuleActionCollection();
				actions.Add(GetNextActionID(), Localization.GetString(ModuleActionType.AddContent, this.LocalResourceFile),
					ModuleActionType.AddContent, "", "", EditUrl(), false, DotNetNuke.Security.SecurityAccessLevel.Edit,
					 true, false);

				return actions;
			}
		}

		#endregion

		protected void Button1_Click(object sender, EventArgs e)
		{
			int i;
			ProductIDError.Text = " ";
			ProductQuantityError.Text = " ";
			Label4.Text = " ";
			Label5.Text = " ";
			if ((TextBox1.Text == "") || (int.TryParse(TextBox1.Text.Trim(), out i) == false))
			{
				ProductIDError.Text = "Wrong Product ID !!";
			}
			if ((TextBox2.Text == "") || (int.TryParse(TextBox2.Text.Trim(), out i) == false))
			{
				ProductQuantityError.Text = "Wrong Product Quantity !!";
			}
			if ((TextBox1.Text != "") && (TextBox2.Text != "") && (int.TryParse(TextBox1.Text.Trim(), out i) == true) && (int.TryParse(TextBox2.Text.Trim(), out i) == true))
			{
				CafeteriaOrderController cntrlr = new CafeteriaOrderController();
				CafeteriaController cntrlr1 = new CafeteriaController();

				Cafeteria cafeteria = new Cafeteria();
				cafeteria.ItemID = Convert.ToInt32(TextBox1.Text);

				if (cntrlr1.GetCafeteria(cafeteria.ItemID) == null)
				{
					ProductIDError.Text = "Wrong Product ID !!";
				}
				if (cntrlr1.GetCafeteria(cafeteria.ItemID) != null)
				{
					CafeteriaOrder cafeteriaOrder = new CafeteriaOrder();
					cafeteriaOrder.ItemID = Convert.ToInt32(TextBox1.Text);
					cafeteriaOrder.ItemQuantity = Convert.ToInt32(TextBox2.Text);
					cafeteriaOrder.Room = TextBox3.Text;
					cafeteriaOrder.TotalPrice = cntrlr1.GetCafeteria(cafeteria.ItemID).ItemPrice * Convert.ToInt32(TextBox2.Text);

					Label4.Text = cafeteriaOrder.TotalPrice.ToString() + " VND";
				}

				this.GridView1.DataBind();
				this.GridView2.DataBind();
			}
		}

		protected void Button2_Click(object sender, EventArgs e)
		{
			int i;
			ProductIDError.Text = " ";
			ProductQuantityError.Text = " ";
			Label4.Text = " ";
			Label5.Text = " ";
			if ((TextBox1.Text == "") || (int.TryParse(TextBox1.Text.Trim(), out i) == false))
			{
				ProductIDError.Text = "Wrong Product ID !!";
			}
			if ((TextBox2.Text == "") || (int.TryParse(TextBox2.Text.Trim(), out i) == false))
			{
				ProductQuantityError.Text = "Wrong Product Quantity !!";
			}
			if ((TextBox1.Text != "") && (TextBox2.Text != "") && (int.TryParse(TextBox1.Text.Trim(), out i) == true) && (int.TryParse(TextBox2.Text.Trim(), out i) == true))
			{
				CafeteriaOrderController cntrlr = new CafeteriaOrderController();
				CafeteriaController cntrlr1 = new CafeteriaController();

				Cafeteria cafeteria = new Cafeteria();
				cafeteria.ItemID = Convert.ToInt32(TextBox1.Text);

				if (cntrlr1.GetCafeteria(cafeteria.ItemID) == null)
				{
					ProductIDError.Text = "Wrong Product ID !!";
				}
				if (cntrlr1.GetCafeteria(cafeteria.ItemID) != null)
				{
					CafeteriaOrder cafeteriaOrder = new CafeteriaOrder();
					cafeteriaOrder.ItemID = Convert.ToInt32(TextBox1.Text);
					cafeteriaOrder.ItemQuantity = Convert.ToInt32(TextBox2.Text);
					cafeteriaOrder.Room = TextBox3.Text;
					cafeteriaOrder.TotalPrice = cntrlr1.GetCafeteria(cafeteria.ItemID).ItemPrice * Convert.ToInt32(TextBox2.Text);

					cafeteriaOrder.Time = DateTime.Now;
					cafeteriaOrder.ByUser = DotNetNuke.Entities.Users.UserController.GetCurrentUserInfo().DisplayName;

					cntrlr.AddOrder(cafeteriaOrder);
					TextBox1.Text = "";
					TextBox2.Text = "";
					TextBox3.Text = "";
					Label4.Text = " ";

					Label5.Text = "Order Online Succeed !";
				}

				this.GridView1.DataBind();
				this.GridView2.DataBind();
			}
		}
	}
}
